package com.indusfo.spc.service.impl;

import com.google.gson.JsonObject;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ClassesMapper;
import com.indusfo.spc.mapper.UploadMapper;
import com.indusfo.spc.pojo.Classes;
import com.indusfo.spc.pojo.ProductLine;
import com.indusfo.spc.service.ClassesService;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import org.apache.poi.util.StringUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author louk
 * @date 2019/10/17 10:05
 * @description 班次定义表service层实现类
 */
@Service
public class ClassesServiceImpl implements ClassesService {

    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private UploadMapper uploadMapper;

    @Override
    public JSONObject queryClasses(Classes classes) {


        Integer pagesize = classes.getPagesize();
        Integer pageindex = classes.getPageindex();
        if(pagesize != null && pageindex != null) {
            classes.setIncept(pagesize*(pageindex - 1));
        }
        List<Classes> classesList = classesMapper.queryClasses(classes);
        if(classesList.isEmpty()){
            return JSONObject.oK("没有相关数据", classesList, 0);
        }
        int i = classesMapper.countClasses(classes);
        return JSONObject.oK("查询成功", classesList,i);
    }

    @Override
    public JSONObject updateClasses(Classes classes) {


        //更新时判断班次ID是否  判断班次名称是否为空并且重复  判断产线是否为空
        if(classes.getlClasses()==null){
            return JSONObject.build(500,"更新时请选择班次ID");
        }
        //判断所传时间格式是否正确
        judge(classes);
        int i = classesMapper.queryIsExist(classes);
        if(i>0){
            return JSONObject.build(500,"班次名称名称已存在");
        }


        int row = classesMapper.updateClasses(classes);
        if(row == 0) {
            return JSONObject.build(500,"数据修改失败");

        }
        return JSONObject.oK("数据修改成功");
    }

    @Override
    public JSONObject defineClasses(Integer[] classes, Integer lDataState) {

        //判断传入的参数是否为null
        if(lDataState == null) {
            //为null抛出异常
            return JSONObject.build(500,"数据状态不能为空");
        }else if(lDataState != 1 && lDataState != 2 && lDataState != 3) {
            return JSONObject.build(500,"数据状态错误");
        }
        if(classes == null || classes.length<=0) {
            return JSONObject.build(500,"请选择删除数据");
        }
        //调用删除的接口
        int row = classesMapper.defineClasses(classes, lDataState);
        //声明msg字符串，用来存抛出的异常
        String msg = "";
        if (row == 0) {
            // 判断传入的数据状态参数lDataState,返回相应信息
            switch (lDataState) {
                case 1:
                    throw new ModifyFailedException("启用失败");
                case 2:
                    throw new ModifyFailedException("删除失败");
                case 3:
                    throw new ModifyFailedException("停用失败");
                default:
                    break;
            }
        } else {
            switch (lDataState) {
                case 1:
                    msg = "启用成功";break;
                case 2:
                    msg = "删除成功";break;
                case 3:
                    msg = "停用成功";break;
                default:
                    break;
            }
        }
        return JSONObject.oK(msg);
    }

    @Override
    public JSONObject insertClasses(Classes classes) {

        //新增时判断班次名称  以及工位是否为空
        if(classes.getlClasses()!=null){
            throw new ParamsErrorException("班次ID不能存在");
        }
        judge(classes);
        //新增时设置数据状态为1
        classes.setlDataState(1);
        int i = classesMapper.queryIsExist(classes);
        if(i>0){
            return JSONObject.build(500,"名称已存在");
        }
        int row = classesMapper.insertClasses(classes);
        if(row == 0) {
            return JSONObject.build(500,"新增设备资料失败");
        }
        return JSONObject.oK("新增成功");
    }

    @Override
    public JSONObject upload(MultipartFile multipartFile) {
        //设置好对应的列为
        //班次名称
        int vcClasses=0;
        //开始时间
        int dBtime=0;
        //时长
        int workingHour=0;
        //工位id
        int staId=0;

        return null;
    }



    public void judge(Classes classes){

        if(classes.getVcClasses().isEmpty()){
            throw new ParamsErrorException("班次名称不能为空");
        }
        if(classes.getlProductionLineId()==null){
            throw new ParamsErrorException("产线不能为空");
        }
        try{
            if("".equals(classes.getdBtime())){
                classes.setdBtime(null);
            }else{
                Time time = Time.valueOf(classes.getdBtime());
            }
        }catch (Exception e){
            throw new ParamsErrorException("班次信息开始时间格式不正确");
        }
        if(classes.getWorkingHour()!=null){

            Double workingHour = classes.getWorkingHour();
            if(workingHour.toString().length()>10){
                throw  new ParamsErrorException("工作时长数字过大");
            }
        }


    }
    @Override
    public String checkNoRepeatExcel(Classes classes) {
        if(StringUtils.isBlank(classes.getVcClasses()) && StringUtils.isBlank(classes.getVcProductionLineName()) && StringUtils.isBlank(classes.getdBtime()) && StringUtils.isBlank(classes.getWorkingHour().toString())){
            return "该行数据为空(原因:清空行内容时未清空彻底/未输入)";
        }
        if(StringUtils.isBlank(classes.getVcClasses())){
            return "班次名称为空";
        }
        if(classes.getVcProductionLineName()==null){
            return "产线名称为空";
        }
        Map<String, ProductLine> productLineMap = uploadMapper.queryProductionLine();
        if(!productLineMap.containsKey(classes.getVcProductionLineName())){
            return "产线名城输入错误/不规范";
        }
        classes.setlProductionLineId(productLineMap.get(classes.getVcProductionLineName()).getLProductionLineId());
        try{

            if(StringUtils.isBlank(classes.getdBtime())){
                classes.setdBtime(null);
            } else{
                Time time = Time.valueOf(classes.getdBtime());
            }
        }catch (Exception e){
            return "班次信息开始时间格式不正确";
        }
        int i = classesMapper.queryIsExist(classes);
        if(i>0){
            return "名称已存在";
        }
        if(classes.getWorkingHour()!=null){
            Double workingHour = classes.getWorkingHour();
            if(workingHour.toString().length()>10){
                throw  new ParamsErrorException("工作时长数字过大");
            }
        }
        return null;
    }

    @Override
    public JSONObject updateListClasses(List<Classes> classesList) {

        int flag = uploadMapper.updateListClasses(classesList);
        if(flag>0){
            return JSONObject.oK("新增成功");
        }
        return JSONObject.oK("新增失败");
    }
}
